/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.bean;

import org.ipti.br.tag.bo.GrantsBO;
import org.ipti.br.tag.entity.Functionality;
import org.ipti.br.tag.entity.Grants;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.inject.Produces;
import javax.faces.component.UIComponentBase;
import javax.inject.Inject;
import javax.inject.Named;
import org.primefaces.component.menubar.Menubar;
import org.primefaces.component.menuitem.UIMenuItem;
import org.primefaces.component.submenu.UISubmenu;

/**
 *
 * @author luancorumba
 */
@Named
@SessionScoped
public class PersonageBarMB extends AppBaseBean implements Serializable {

    @Inject
    private GrantsBO grantsBO;
    @Inject
    private SessionBean sessionBean;
    @Inject
    private transient Menubar actorMenu;
    
    public PersonageBarMB() {
    }
    
    @Produces
    public Menubar getMenuBar(){
        Menubar bar;
        bar = (Menubar) getFacesContext().getApplication().createComponent(Menubar.COMPONENT_TYPE);
        bar.setId("actorMenuBar");
        bar.setAutoDisplay(true);
        
        Map<Functionality, UIComponentBase> funcs = new HashMap<Functionality, UIComponentBase>(),
                                            roots = new HashMap<Functionality, UIComponentBase>();
        Functionality func;
        UIComponentBase component, temp = null;
        for (Grants grants : grantsBO.grantsPerPersonage(sessionBean.getLoggedActor().getPersonage().getId())) {
            func = grants.getFunctionality();
            component = makeMenuItem(func);
            while(func.getFather() != null && !funcs.containsKey(func.getFather())){
                temp = makeSubMenu(func.getFather());
                temp.getChildren().add(component);
                component = temp;
                funcs.put(func.getFather(),component);
                func = func.getFather();
            }
            if(funcs.containsKey(func.getFather())){
                temp = funcs.get(func.getFather());
                temp.getChildren().add(component);
            }else{
                roots.put(func, component);
            }
        }
        
        bar.getChildren().addAll(roots.values());
        return bar;
    }
    
    private UIMenuItem makeMenuItem(Functionality func){
        UIMenuItem menuItem = (UIMenuItem) getFacesContext().getApplication().createComponent(UIMenuItem.COMPONENT_TYPE);
        menuItem.setId("item" + func.getId());
        menuItem.setValue(func.getName());
        menuItem.setUrl(getDOMAIN() + func.getCallPage());
        return menuItem;
    }
    
    private UISubmenu makeSubMenu(Functionality func){
        UISubmenu submenu = (UISubmenu) getFacesContext().getApplication().createComponent(UISubmenu.COMPONENT_TYPE);
        submenu.setId("submenu" + func.getId());
        submenu.setLabel(func.getName());
        return submenu;
    }

    public Menubar getActorMenu() {
        return actorMenu;
    }

    public void setActorMenu(Menubar actorMenu) {
        this.actorMenu = actorMenu;
    }
}
